#include "nodeoperation.h"

NodeOperation& NodeOperation::getInstance()
{
	static NodeOperation oper;
	return oper;
}

void NodeOperation::selectTodoNodes()
{
	auto initNodeFunc = [](ContentNode& contentNode,QSqlRecord& record) {
		int id = record.value("node_id").toInt();
		int parentId = record.value("node_parentid").toInt();
		int type = record.value("node_type").toInt();
		QString text = record.value("node_text").toString();
		int sort = record.value("node_sort").toInt();
		contentNode.init(id,parentId,type,sort,text);
		contentNode.initView();
		};
	auto nodeList = SqlConnection::getInstance().selectAllFromTable("todo_node", static_cast<SqlConnection::SelectCallBackFunc<ContentNode>>(initNodeFunc));
	for (ContentNode* node : nodeList) {
		int parentId = node->getParentId();
		m_todoNodes[parentId].push_back(node);
	}
	for (auto&& nodeList : m_todoNodes) {
		nodeList.second.sort([](const BaseNode* node1,const BaseNode* node2) {
			return node1->getSort() > node2->getSort();
			});
	}
}

std::list<BaseNode*> NodeOperation::getNodesByDirId(int dirId)
{
	if (m_todoNodes.count(dirId) == 0) {
		return std::list<BaseNode*>();
	}
	return m_todoNodes[dirId];
}
